'use strict';
angular.module('app.controllers')
	.controller('RootCtrl', function($scope, $state, $window) {
		$scope.userRole = $window.sessionStorage.userRole;
		$scope.role = $window.sessionStorage.subrole;
		// if (!$window.sessionStorage.userRole) {
		// 	$state.parent = $state.current;
		// 	$state.go('login', {
		// 		'previous': $state.parent.name
		// 	});
		// } else {
			$scope.view = $state.current;
			$scope.go = $state.go;
			$scope.isActive = function(view_name) {
				var active = ($scope.view.name == view_name);
				return active;
			}

			$scope.$on('$stateChangeSuccess', function(event, toState) {
				$scope.activeState = toState.name;
			});
		// }
	})
	.controller('SideNavCtrl', function($scope, $state, $window) {
		var vm = this;
		vm.userRole = $window.sessionStorage.userRole;
	})
	.controller('navCtrl', function($scope, $state, $window, ngDialog, UserService) {
		var vm = this;
		vm.userRole = $window.sessionStorage.userRole;
		vm.hotelName = $window.sessionStorage.hotelName;

		// if (vm.userRole) {
		// 	UserService.get($window.sessionStorage.userId, vm.userRole).then(function(ret) {
		// 		vm.user = ret.data;
		// 	});

		// }
		// UserService.isLoggedin().then(function(data) {
		// 	if (data.data != "OK" && $state.$current.self.url != "/login") {
		// 		alert("会话已失效，请重新登录！");
		// 		$state.go('login', null, {
		// 			reload: false
		// 		});
		// 	}
		// });
		vm.logout = function() {
			// UserService.logout().then(function(ret) {
			$window.sessionStorage.logged = false;
			$window.sessionStorage.userId = '';
			$window.sessionStorage.userRole = '';
			$window.sessionStorage.hotelName = '';
			$state.go('login');
			// });
		};

		vm.changePassword = function() {

			ngDialog.open({
				template: 'views/change_password.html',
				ariaAuto: false,
				className: 'ngdialog-theme-plain custom-width',
				$scope: $scope
			})
		}

	})
	